
function isWeixn() {
    var ua = navigator.userAgent.toLowerCase();
    return ua.match(/MicroMessenger/i) == "micromessenger";
}


var $bg = $('.bg'),
    $video = $('#video'),
    elVideo = $video[0],
    $iptCardNum = $('#ipt-cardnum'),
    $btnSubmit = $('#btn-submit'),
    $cardnumBox = $('#cardnum-box'),
    $jumpLink = $('.jump-link'),
    $jumpLinks = $('#jump-links'),
    $share = $('#share'),
    $shareWap = $('#share-wap'),
    $liveLink = $('.live-link'),
    $sleep1Img = $('#sleep1').find('img'),
    $sleep2Img = $('#sleep2').find('img'),
    $sleep3Img = $('#sleep3').find('img'),
    $notices = $('#notices'),
    playFlag = false;

// 修正自适应定位
var timer = setInterval(function() {
    var iptCardNumLeft = parseInt($bg.width() / (640 / 40)) + 'px',
        iptCardNumWidth = parseInt($bg.width() / (640 / 250)) + 'px',
        btnCardNumLeft = parseInt($bg.width() / (640 / 310)) + 'px',
        btnCardNumWidth = parseInt($bg.width() / (640 / 115)) + 'px',
        linkJumpHeight = parseInt($jumpLinks.height() / (400 / 170)) + 'px',
        linkJumpTop = parseInt($jumpLinks.height() / (400 / 185)) + 'px',
        sleep1Width = parseInt($bg.width() / (640 / 185)) + 'px',
        sleep2Width = parseInt($bg.width() / (640 / 205)) + 'px',
        sleep3Width = parseInt($bg.width() / (640 / 240)) + 'px';

    if ($iptCardNum.css('margin-left') == iptCardNumLeft && $iptCardNum.css('width') == iptCardNumWidth) {
        if ($btnSubmit.css('margin-left') == btnCardNumLeft && $btnSubmit.css('width') == btnCardNumWidth) {
            if ($iptCardNum.height() > 0) {
                if ($sleep1Img.css('width') == sleep1Width) {
                    if ($sleep2Img.css('width') == sleep2Width) {
                        if ($sleep3Img.css('width') == sleep3Width) {
                            clearInterval(timer);
                        }
                    }
                }
            }
        }
    }

    $video.css({
        'margin-top': $('#sleep1').height() - $video.height()
    });

    // 羊1
    $sleep1Img.css({
        'margin-left': parseInt($bg.width() / (640 / 450)),
        width: sleep1Width,
        display: 'block'
    });

    // 羊2
    $sleep2Img.css({
        width: sleep2Width,
        display: 'block'
    });

    // 羊3
    $sleep3Img.css({
        width: sleep3Width,
        'margin-top': parseInt($bg.height() / (200 / 65)),
        display: 'block'
    });

    // 卡号输入框
    $iptCardNum.css({
        'margin-left': iptCardNumLeft,
        width: iptCardNumWidth,
        height: $cardnumBox.height() - 4,
        'margin-top': 3
    });

    // 卡号提交按钮
    $btnSubmit.css({
        'margin-left': btnCardNumLeft,
        'margin-top': 2,
        width: btnCardNumWidth,
        height: $cardnumBox.height() - 4
    });

    // 底部跳转按钮
    $jumpLink.css({
        'margin-top': linkJumpTop,
        height: linkJumpHeight
    });
}, 100);

// 播放统计
function playStat() {
    $.post('/api/playStat');
}

function modalAlert($content) {
    var modal = '<div class="modal fade modal-alert">\
                    <div class="modal-dialog" style="width: 70%; margin:50px auto; z-index: 999;">\
                    </div>\
                </div>',
        $modal = $(modal);

    $content.click(function() {
        $modal.modal('hide');
    });

    $modal.find('.modal-dialog').html($content);

    $modal.modal().on('hidden.bs.modal', function() {
        $modal.remove();
    });
}

// 进入直播页
$liveLink.click(function() {
    var $self = $(this);

    $.get('/api/liveSwitch', function(resp) {
        if (resp.code == 0) {
            var $liveNoticeBox = $('#live-notice-box'),
                $img = null;

            if (resp.data.liveSwitch) {
                if ($self.data('live') == 2 && !isWeixn()) {
                    $img = $liveNoticeBox.find('img.wap').clone(true);
                    modalAlert($img);
                } else {
                    var card = $iptCardNum.val();
                    location.href = $self.data('href') + '?card=' + card;
                }
            } else {
                if (!isWeixn() && $self.data('live') == 2) {
                    $img = $liveNoticeBox.find('img.wap').clone(true);
                } else {
                    $img = $liveNoticeBox.find('img.deny').clone(true);
                }

                modalAlert($img);
            }
        } else {
            alert(resp.msg || '服务器异常！');
        }
    });
});

// 播放
$video.click(function() {
    if (elVideo.paused) {
        if (!playFlag) {
            playFlag = !playFlag;
            playStat();
        }

        elVideo.play();
    } else {
        elVideo.pause();
    }
});

// 参与抽奖确认按钮
$btnSubmit.click(function() {
    if (!$iptCardNum.val()) {
        modalAlert($notices.find('img.error').clone(true));
    } else {
        $.ajax({
            url: '/api/memberCard',
            data: {cardNum: $iptCardNum.val()},
            success: function(resp) {
                switch (resp.code) {
                    case 0:
                        modalAlert($notices.find('img.success').clone(true));
                        break;

                    case -3:
                        modalAlert($notices.find('img.error').clone(true));
                        break;

                    case -6:
                        modalAlert($notices.find('img.end').clone(true));
                        break;

                    case -7:
                        modalAlert($notices.find('img.repeat').clone(true));
                        break;

                    default:
                        alert(resp.msg || '网络异常！');
                        break;
                }
            },
            error: function(xhr) {
                console.log(xhr);
                alert('服务器繁忙！');
            }
        });
    }
});

// 关闭分享
$share.click(function() {
    $(this).addClass('hide');
});

$shareWap.click(function() {
    $(this).addClass('hide');
});

// 底部页面跳转
$jumpLink.click(function() {
    switch ($(this).data('stat')) {
        case 'link1':
            $.ajax({url: '/api/jumpLink1Stat', async: false});
            if (isWeixn()) {
                $share.removeClass('hide');
            } else {
                $shareWap.removeClass('hide');
            }
            break;

        case 'link2':
            $.ajax({url: '/api/jumpLink2Stat', async: false});
            window.location.href = $(this).data('link');
            break;

        default:
            break;
    }

    return false;
});

$iptCardNum.keypress(function(event) {
    var keyCode = event.keyCode;
    event.returnValue = keyCode >= 48 && keyCode <= 57;
});

var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE') != -1;
if (isIPHONE) {
    $(window).on('touchstart', function(event) {
        if ($(event.target).attr('id') != 'ipt-cardnum' && $iptCardNum.is(':focus')) {
            $iptCardNum.blur();
        }
    });
}

// 页面打开次数统计
$.post('/api/browseStat');